package urna;

import java.awt.Component;
import java.awt.EventQueue;
import java.io.File;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Salida {
	
	private JFrame frame;
	private static Registry registry;
	private int port;

	public Salida(int port){
		this.port = port; //1099
		frame = new JFrame();
		frame.setVisible(true);
		frame.setBounds(100,100,500,100);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public boolean enviar(String ip, String path){
		File candidatos = new File(path);
		try {
			registry = LocateRegistry.getRegistry(ip,port);
			InterfaceCRV crv = (InterfaceCRV) registry.lookup("CRV");
			return crv.enviarCandidatos(candidatos);
		} catch (Exception e) {
			return false;
		}
		
	}
	
	public JFrame getFrame() {
		return frame;
	}
	
	public void mostrar(Component c){
		frame.add(c);
		frame.paintComponents(frame.getGraphics());
	}
	
	public static void main(final String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					if (args.length > 1) {
						Salida salida = new Salida(Integer.parseInt(args[0]));
						Log log = new Log();
						Seguridad seguridad = new Seguridad(log, salida);
						GestordeDatos gestor = new GestordeDatos(log, seguridad, salida, Integer.parseInt(args[1]));
						Configuracion configuracion = new Configuracion(log, gestor,seguridad,salida);
						Entrada entrada = new Teclado(configuracion, gestor);
						salida.frame.addKeyListener((Teclado)entrada);
						System.out.println("Urna inicializada correctamente (portCRV = "+args[0]+", portLector = "+args[1]+")");
					} else {
						System.out.println("Faltan parametros para la inicializacion de la Urna. Parametro1=puerto (1099), Parametro2=puerto (1000)");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

}
